package nju.firework.game.main;

import nju.firework.res.ResType;
import nju.firework.res.ResourceManager;
import nju.firework.scene.GameScene;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.entity.shape.Shape;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

import android.hardware.SensorManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class MainActivity extends BaseGameActivity {

	private GameScene gameScene;
	public PhysicsWorld physicsWorld;
	public ITiledTextureRegion heroPic;
	public ResourceManager resManager;
	
	public EngineOptions onCreateEngineOptions() {
		// TODO Auto-generated method stub
		Camera mCamera = new Camera(0, 0, MainConst.CAMERA_WIDTH, MainConst.CAMERA_HEIGHT);
		return new EngineOptions(true,ScreenOrientation.LANDSCAPE_FIXED,
				new FillResolutionPolicy(), mCamera);
	}

	public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallBack)
			throws Exception {
		// TODO Auto-generated method stub
		resManager = new ResourceManager(this);
		resManager.setBase("hero/");
		resManager.loadTiledTexture("hero.png", ResType.HERO, "hero", 0, 0, 5, 6);
		resManager.setBase("cell/");
		resManager.loadTexture("floor.png", ResType.CELL, "floor", 0, 0);
		resManager.loadTexture("block.png", ResType.CELL, "block", 0, 100);
		resManager.endLoad();
		pOnCreateResourcesCallBack.onCreateResourcesFinished();
	}

	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
		// TODO Auto-generated method stub
		physicsWorld=new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH), true);
//		final Shape ground = new Rectangle(0, MainConst.CAMERA_HEIGHT - 2, MainConst.CAMERA_WIDTH, 2, getVertexBufferObjectManager());
//		final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
//		Body shapeBody = PhysicsFactory.createBoxBody(this.physicsWorld, (IAreaShape) ground, BodyType.StaticBody, wallFixtureDef);
//		this.physicsWorld.registerPhysicsConnector(new PhysicsConnector((IAreaShape) ground, shapeBody, true, false));
		gameScene = new GameScene(this);
		gameScene.registerUpdateHandler(physicsWorld);
		pOnCreateSceneCallback.onCreateSceneFinished(gameScene);
	}

	public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback)
			throws Exception {
		// TODO Auto-generated method stub
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

    
}
